package com.yeswater.redis7.controller;

import com.yeswater.redis7.entity.Customer;
import com.yeswater.redis7.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Random;

/**
 * @Description:
 * @author： yeswater
 * @create： 2024/6/28
 */

@Api(tags = "客户Customer接口+布隆过滤器")
@RestController
@Slf4j
public class CustomerController {
	@Resource
	private CustomerService customerService;
	
	@ApiOperation("数据库初始化2条Customer数据")
	@RequestMapping(value = "/customer/add", method = RequestMethod.POST)
	public void addCustomer() {
		for (int i = 0; i < 2; i++) {
			Customer customer = new Customer();
			int uid = new Random().nextInt(9000) + 1000;
			customer.setId(uid);
			customer.setCname("customer" + uid);
			customer.setAge(new Random().nextInt(30) + 1);
			customer.setPhone("1381111xxxx");
			customer.setSex((byte) new Random().nextInt(2));
			customer.setBirth(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
			customerService.addCustomer(customer);
		}
	}
	
	@ApiOperation("单个用户查询，按customer id查用户信息")
	@RequestMapping(value = "/customer/{id}", method = RequestMethod.GET)
	public Customer findCustomerById(@PathVariable int id) {
		return customerService.findCustomerById(id);
	}
	
	@ApiOperation("BloomFilter案例")
	@RequestMapping(value = "/customerbloomfilter/{id}", method = RequestMethod.GET)
	public Customer findCustomerByIdWithBloomFilter(@PathVariable int id) {
		return customerService.findCustomerByIdWithBloomFilter(id);
	}
}
